/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * login.java
 *
 * Created on Oct 15, 2012, 3:26:38 PM
 */
package gui;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

import pms.Database;
import pms.Employee;
import pms.Manager;
import pms.PayrollAdmin;
import pms.Supervisor;

/**
 * 
 * @author moonn52
 */
public class login extends javax.swing.JFrame implements ActionListener {

	/** Creates new form login */
	private Employee e;
	
	public login() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jPasswordField2 = new javax.swing.JPasswordField();
		jButton1 = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jLabel1.setText("User:");

		jLabel2.setText("Password:");

		jButton1.setText("Login");
		jButton1.addActionListener(this);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(
												layout.createSequentialGroup()
												.addGroup(
														layout.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
																.addComponent(
																		jLabel2)
																		.addComponent(
																				jLabel1))
																				.addPreferredGap(
																						javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																						.addGroup(
																								layout.createParallelGroup(
																										javax.swing.GroupLayout.Alignment.LEADING,
																										false)
																										.addComponent(
																												jTextField1)
																												.addComponent(
																														jPasswordField2,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														185,
																														javax.swing.GroupLayout.PREFERRED_SIZE)))
																														.addGroup(
																																javax.swing.GroupLayout.Alignment.TRAILING,
																																layout.createSequentialGroup()
																																.addPreferredGap(
																																		javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																																		99,
																																		javax.swing.GroupLayout.PREFERRED_SIZE)
																																		.addComponent(
																																				jButton1)
																																				.addGap(100,
																																						100,
																																						100)))
																																						.addContainerGap(
																																								javax.swing.GroupLayout.DEFAULT_SIZE,
																																								Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
						.addGap(25, 25, 25)
						.addGroup(
								layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(
												jTextField1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel1))
												.addGap(18, 18, 18)
												.addGroup(
														layout.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
																.addComponent(jLabel2)
																.addComponent(
																		jPasswordField2,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addGap(28, 28, 28)
																		.addComponent(jButton1)
																		.addContainerGap(
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(login.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(login.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(login.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(login.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new login().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton jButton1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JPasswordField jPasswordField2;

	// End of variables declaration//GEN-END:variables
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		boolean validLogin = true;
		try
		{
			Database db = new Database();
			String username = jTextField1.getText();
			String password = jPasswordField2.getText();
			
			e = new Employee(username, password);
			if (Employee.accessLevel.fromInteger(e.getPriveligeLevel()) == Employee.accessLevel.SUPERVISOR)
				e = new Supervisor(username, password);
			else if (Employee.accessLevel.fromInteger(e.getPriveligeLevel()) == Employee.accessLevel.PAYROLL_ADMIN)
				e = new PayrollAdmin(username, password);
			else if (Employee.accessLevel.fromInteger(e.getPriveligeLevel()) == Employee.accessLevel.MANAGER)
				e = new Manager(username, password);
		}
		catch (SQLException e) {
			Object frame = null;
			JOptionPane.showMessageDialog((Component) frame, "Either the username or password was incorrect.");
			validLogin = false;}
		catch (Exception e) {
			Object frame = null;
			JOptionPane.showMessageDialog((Component) frame, e.getMessage());
			validLogin = false;}
		if(validLogin)
		{
			new schedule(e).setVisible(true);
			this.setVisible(false);
		}
		/*if (arg0.getID() == 1001) {
			if (jTextField1.getText().equals("nicmoon") && jPasswordField2.getText().equals("pass")) {
				new schedule().setVisible(true);
				this.setVisible(false);
			}*/
	}
}
